package com.pureut.quality.service.impl;


import com.pureut.common.security.utils.DictUtils;
import com.pureut.quality.domain.dto.*;
import com.pureut.quality.mapper.QualityInspectionDocumentMapper;
import com.pureut.quality.service.QualityKanBanService;
import com.pureut.quality.utils.DateCalendarUtils;
import com.pureut.system.api.domain.SysDictData;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: CM
 * @date: 2023/5/8  14:45
 */
@Service
public class QualityKanBanServiceImpl implements QualityKanBanService {

    //品质检验单据Mapper
    @Resource
    private QualityInspectionDocumentMapper qualityInspectionDocumentMapper;

    /**
     * 检验单据统计
     * @return
     */
    @Override
    public InspectionDocumentStatisticsDto inspectionDocumentStatistics() {

        //检验单总数
        long sumInspection = qualityInspectionDocumentMapper.getSumInspection();

        //48小时内待检验,待审核,待处理的单据个数
        InspectionDocumentStatisticsDto inspectionDocumentStatisticsDto =  qualityInspectionDocumentMapper.getInspectionNum();

        //逾期未检验单据
        Long overdue = qualityInspectionDocumentMapper.getOverdue();

        inspectionDocumentStatisticsDto.setOverdue(overdue);
        inspectionDocumentStatisticsDto.setSumInspection(sumInspection);



        return inspectionDocumentStatisticsDto;
    }

    /**
     * 48小时内检验单据信息
     * @return
     */
    @Override
    public List<inspectionHoursInfoDto> inspectionHoursInfo() {
        List<inspectionHoursInfoDto> list = qualityInspectionDocumentMapper.inspectionHoursInfo();

        String inspectionPlan="";

        for (inspectionHoursInfoDto inspectionHoursInfoDto : list) {
            //检测阶别
            List<SysDictData> rankArray = DictUtils.getDictCache("item_rank");
            Map<String, String> rankMap = rankArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

            //单据状态
            List<SysDictData> documentStatusArray = DictUtils.getDictCache("quality_document_status");
            Map<String, String> documentStatusMap = documentStatusArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

            //检验结果
            List<SysDictData> resultsArray = DictUtils.getDictCache("quality_inspection_results");
            Map<String, String> resultsMap = resultsArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

            inspectionHoursInfoDto.setItemRank(rankMap.get(inspectionHoursInfoDto.getItemRank()));
            inspectionHoursInfoDto.setInspectionResults(resultsMap.get(inspectionHoursInfoDto.getInspectionResults()));
            inspectionHoursInfoDto.setStatus(documentStatusMap.get(inspectionHoursInfoDto.getStatus()));

            //计算检验进度
            BigDecimal currentCount1 = new BigDecimal(inspectionHoursInfoDto.getCount());
            BigDecimal totalCount1 = new BigDecimal(inspectionHoursInfoDto.getQuantityInspected());
            if (totalCount1.equals(BigDecimal.ZERO)){
                inspectionPlan = "0.0";
            }else{
                BigDecimal divide1 = currentCount1.divide(totalCount1,2, BigDecimal.ROUND_HALF_UP);

                inspectionPlan = divide1.multiply(new BigDecimal(100)).toString();
            }

            inspectionHoursInfoDto.setInspectionPlan(inspectionPlan);
        }
        return list;
    }

    /**
     * 待操作单据统计
     * @return
     */
    @Override
    public List<DocumentStaticsDto> documentStatics() {

        List<DocumentStaticsDto> list = new ArrayList<>();

        DocumentStaticsDto documentStaticsDto = qualityInspectionDocumentMapper.selectDocumentStatus("1");
        documentStaticsDto.setItemRank("IQC");

        DocumentStaticsDto documentStaticsDto1 = qualityInspectionDocumentMapper.selectDocumentStatus("2");
        documentStaticsDto1.setItemRank("FAI");

        DocumentStaticsDto documentStaticsDto2 = qualityInspectionDocumentMapper.selectDocumentStatus("3");
        documentStaticsDto2.setItemRank("IPQC");

        DocumentStaticsDto documentStaticsDto3 = qualityInspectionDocumentMapper.selectDocumentStatus("4");
        documentStaticsDto3.setItemRank("FQC");

        DocumentStaticsDto documentStaticsDto4 = qualityInspectionDocumentMapper.selectDocumentStatus("5");
        documentStaticsDto4.setItemRank("OQC");

        list.add(documentStaticsDto);
        list.add(documentStaticsDto1);
        list.add(documentStaticsDto2);
        list.add(documentStaticsDto3);
        list.add(documentStaticsDto4);

        return list;
    }

    @Override
    public SevenDayYieldDto sevenDayYield() {

        //获取当前时间之前的7天
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(new Date());
        ArrayList<String> arrayList = DateCalendarUtils.pastDay(format);

        ArrayList<String> faiList = new ArrayList<String>();
        ArrayList<String> iqcList = new ArrayList<String>();
        ArrayList<String> fqcList = new ArrayList<String>();
        ArrayList<String> oqcList = new ArrayList<String>();
        ArrayList<String> ipqcList = new ArrayList<String>();

        String resultA="";
        String resultFai="";
        String resultIpqc="";
        String resultFqc="";
        String resultOqc="";

        for (String date : arrayList) {
            //根据阶别和当前时间查询出合格数量
         long yield =  qualityInspectionDocumentMapper.getYield(date,"1");
            //根据阶别和当前时间查询出总数量
         long sumRank = qualityInspectionDocumentMapper.getSumRank(date,"1");
            BigDecimal currentCount = new BigDecimal(yield);
            BigDecimal totalCount = new BigDecimal(sumRank);

            if (totalCount.equals(BigDecimal.ZERO)){
                resultA = "0";
            }else{
                BigDecimal divide = currentCount.divide(totalCount,2, BigDecimal.ROUND_HALF_UP);
                //甲班合格率
                resultA = divide.multiply(new BigDecimal(100)).toString();
            }

            iqcList.add(resultA);

            //根据阶别和当前时间查询出合格数量
            long faiYield =  qualityInspectionDocumentMapper.getYield(date,"2");
            //根据阶别和当前时间查询出总数量
            long faiSumRank = qualityInspectionDocumentMapper.getSumRank(date,"2");
            BigDecimal faiYieldBig = new BigDecimal(faiYield);
            BigDecimal faiSumRankBig = new BigDecimal(faiSumRank);

            if (faiSumRankBig.equals(BigDecimal.ZERO)){
                resultFai = "0";
            }else{
                BigDecimal divideFai = faiYieldBig.divide(faiSumRankBig,2, BigDecimal.ROUND_HALF_UP);
                //甲班合格率
                resultFai = divideFai.multiply(new BigDecimal(100)).toString();
            }

            faiList.add(resultFai);

            //根据阶别和当前时间查询出合格数量
            long ipqcYield =  qualityInspectionDocumentMapper.getYield(date,"3");
            //根据阶别和当前时间查询出总数量
            long ipqcSumRank = qualityInspectionDocumentMapper.getSumRank(date,"3");
            BigDecimal ipqcYieldBig = new BigDecimal(ipqcYield);
            BigDecimal ipqcSumRankBig = new BigDecimal(ipqcSumRank);

            if (ipqcSumRankBig.equals(BigDecimal.ZERO)){
                resultIpqc = "0";
            }else{
                BigDecimal divideIpqc = ipqcYieldBig.divide(ipqcSumRankBig,2, BigDecimal.ROUND_HALF_UP);
                //甲班合格率
                resultIpqc = divideIpqc.multiply(new BigDecimal(100)).toString();
            }

            ipqcList.add(resultIpqc);

            //根据阶别和当前时间查询出合格数量
            long fqcYield =  qualityInspectionDocumentMapper.getYield(date,"4");
            //根据阶别和当前时间查询出总数量
            long fqcSumRank = qualityInspectionDocumentMapper.getSumRank(date,"4");
            BigDecimal fqcYieldBig = new BigDecimal(fqcYield);
            BigDecimal fqcSumRankBig = new BigDecimal(fqcSumRank);

            if (fqcSumRankBig.equals(BigDecimal.ZERO)){
                resultFqc="0";
            }else{
                BigDecimal divideFqc = fqcYieldBig.divide(fqcSumRankBig,2, BigDecimal.ROUND_HALF_UP);
                //甲班合格率
                resultFqc = divideFqc.multiply(new BigDecimal(100)).toString();
            }

            fqcList.add(resultFqc);


            //根据阶别和当前时间查询出合格数量
            long oqcYield =  qualityInspectionDocumentMapper.getYield(date,"4");
            //根据阶别和当前时间查询出总数量
            long oqcSumRank = qualityInspectionDocumentMapper.getSumRank(date,"4");
            BigDecimal oqcYieldBig = new BigDecimal(oqcYield);
            BigDecimal oqcSumRankBig = new BigDecimal(oqcSumRank);

            if (oqcSumRankBig.equals(BigDecimal.ZERO)){
                resultOqc = "0";
            }else {
                BigDecimal divideOqc = oqcYieldBig.divide(oqcSumRankBig,2, BigDecimal.ROUND_HALF_UP);
                //甲班合格率
                resultOqc = divideOqc.multiply(new BigDecimal(100)).toString();
            }


            oqcList.add(resultOqc);
        }
        SevenDayYieldDto sevenDayYieldDto = new SevenDayYieldDto();
        sevenDayYieldDto.setRiQi(arrayList);
        sevenDayYieldDto.setIqcYield(iqcList);
        sevenDayYieldDto.setFaiYield(faiList);
        sevenDayYieldDto.setIpqcYield(ipqcList);
        sevenDayYieldDto.setOqcYield(oqcList);
        sevenDayYieldDto.setFqcYield(fqcList);

        return sevenDayYieldDto;
    }

    /**
     * 前五大不良
     * @return
     */
    @Override
    public TopFiveBan1Dto topFiveBad() {

        TopFiveBan1Dto topFiveBan1Dto = new TopFiveBan1Dto();


        List<TopFiveBadDto> list1 =qualityInspectionDocumentMapper.getBadInspection();
        //检测阶别
        List<SysDictData> rankArray = DictUtils.getDictCache("item_rank");
        Map<String, String> rankMap = rankArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));
        for (TopFiveBadDto topFiveBadDto : list1) {
            topFiveBadDto.setItemRank(rankMap.get(topFiveBadDto.getItemRank()));
        }

        if (list1.size()!=0){
            topFiveBan1Dto.setTopFiveBadDto(list1);
        }
        return topFiveBan1Dto;
    }


}
